package com.oceansoft.mobile.econsole.aop;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.oceansoft.mobile.econsole.modules.base.entity.Result;
import nl.bitwalker.useragentutils.Browser;
import nl.bitwalker.useragentutils.OperatingSystem;
import nl.bitwalker.useragentutils.UserAgent;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;
import org.apache.log4j.MDC;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * Created by jin.xu 2015/3/9 16:38.
 */
@Component
@Aspect
public class ApicallAspect {
    @Autowired
    private HttpServletRequest request;

    private final Logger client = Logger.getLogger("client");

    @Around("execution(* com.oceansoft.mobile.econsole.modules.api.controller.*.*(..))")
    public Object beforeMethod(ProceedingJoinPoint pjp) throws Throwable {
        Object object = pjp.proceed();
        String result = "";
        boolean isSucc = false;

        try {
            if (object instanceof Result) {
                Result cpresult = (Result) BeanUtils.cloneBean(object);
                cpresult.setData(null);
                result = JSON.toJSONString(cpresult);
                isSucc = cpresult.isSucc();
            } else {
                result = JSON.toJSONString(object);
                isSucc = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            UserAgent agent = ((UserAgent) request.getAttribute("USER_AGENT_OBJ"));
            if (!Objects.isNull(agent)) {
//String userId = request.getParameter("USER_ID");
                //String userName = request.getParameter("USER_NAME");
                MDC.put("ip", request.getRemoteHost());
                MDC.put("os", agent.getOperatingSystem());
                MDC.put("os_n", agent.getOperatingSystem().getName());
                MDC.put("dt", agent.getOperatingSystem().getDeviceType());
                Browser browser = agent.getBrowser();
                if (null != browser) {
                    MDC.put("url", request.getRequestURL());
                    MDC.put("bv", null == agent.getBrowserVersion() ? "" : agent.getBrowserVersion());
                    MDC.put("bn", agent.getBrowser().getName());
                    MDC.put("bg", agent.getBrowser().getGroup());
                    MDC.put("bt", agent.getBrowser().getBrowserType());
                } else {
                    MDC.put("bv", "");
                    MDC.put("bn", "");
                    MDC.put("bg", "");
                    MDC.put("bt", "");
                }
                MDC.put("class", pjp.getTarget().getClass().getName());
                MDC.put("method", pjp.getSignature().getName());
                MDC.put("result", result);
//            MDC.put("args", pjp.getArgs().length > 0 ? JSON.toJSONString(pjp.getArgs()) : "");
                MDC.put("succ", isSucc);
//            client.info(request.getRequestURL());
            }
        }
        return object;
    }
}
